<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    <ui:composition template="/template.xhtml">
        <ui:define name="title">
            <h:outputLabel value="#{messages.Knowledge}"/>
        </ui:define>
        <ui:define name="body">
            <h:outputScript library="javaScript" name="d3.v4.js" target="head"/>
            <h:outputScript library="javaScript" name="graphlib-dot.js" target="head"/>
            <h:outputScript library="javaScript" name="dagre-d3.js" target="head"/>
            <h:outputStylesheet library="css" name="dagre.css"/>
            <h:form styleClass="jsfcrud_list_form" id="myForm">
                <h:panelGrid columns="1">
                    <h:outputLabel value="#{messages.Already}#{messages.Input}:"/>
                    <h:panelGrid columns="2" bgcolor="white">
                        <h:outputLabel value="#{messages.Name}"   dir="RTL"/>
                        <h:inputText id="name" value="#{knowledgeController.selected.name}" title="#{messages.Name}" 
                                     required="true" requiredMessage="#{messages.Name}" maxlength="32"/>
                        <h:outputLabel value="#{messages.Detail}" dir="RTL"/>
                        <h:panelGrid columns="1">
                            <h:outputLabel escape="true" value="输入Markdown文本，使用 '\\('和'\\)'包围行内公式，使用'$$'和'$$'包围行间公式"</h:outputLabel>
                            <h:inputTextarea id="markdown-input"  value="#{knowledgeController.selected.details}"  required="true"
                                             p:placeholer="输入Markdown文本，使用 '\\('和'\\)'包围行内公式，使用'$$'和'$$'包围行间公式#{messages.Nomorethan}10240" onblur="convertMarkdown()" />
                            <div id="markdown-output"></div> 
                        </h:panelGrid>
                        <h:outputLabel value="#{messages.Level}" dir="RTL"/>
                        <h:selectOneMenu id="levelnumber" value="#{knowledgeController.selected.levelnumber}" required="true">
                            <f:selectItem itemLabel="#{messages.Please}#{messages.Select}"/>
                            <f:selectItems value="#{knowledgeController.levelMap.entrySet()}" var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}"/>
                        </h:selectOneMenu>
                    </h:panelGrid>
                    <hr/>                   
                    <!--<embed src="/PersonalizedLearning-war/faces/javax.faces.resource/images/knowledge/3.svg"  type="image/svg+xml" />-->
                    <h:outputLabel value="#{messages.Upload}#{messages.Learningresource}"/>
                    <h:panelGroup style="float: left">
                        <!-- 连接到在知识点中新建学习资源，包括图像、音频和视频等 -->
                        <c:forEach items="#{learningresourceControllerA.mediaMap}" var="resource">
                            <h:commandButton action="#{learningresourceController.prepareAndUpdate(resource.key)}" 
                                             value="#{messages.Add}#{resource.value}"/>&nbsp;&nbsp;
                        </c:forEach>
                    </h:panelGroup>
                    <hr/>
                    <!--<embed src="/PersonalizedLearning-war/faces/javax.faces.resource/images/knowledge/4.svg" type="image/svg+xml" />-->
                    <!--<h:outputLabel value="2. #{messages.No}#{messages.Upload}"/>-->
                    <h:panelGroup style="float: left">
                        <h:commandButton value="#{messages.Edge}#{messages.Maintain}"
                                         action="#{knowledgeController.saveAnd2EdgeamongKnowledge()}"/>
                        &nbsp;
                        <h:commandButton value="#{messages.Save} "
                                         action="#{knowledgeController.saveNow()}"/>
                    </h:panelGroup>
                </h:panelGrid>

                <!--因为当前正在新建知识点，其前驱后继尚未确定-->
                <h:inputHidden p:id="inputGraph" p:name="inputGraph" value="#{knowledgeController.getKnowledgeRelationship4dagre()}"/>

                <svg width="960" height="600"> <g/></svg>
                <script>
                    var svg = d3.select("svg"),
                            inner = d3.select("svg g"),
                            zoom = d3.zoom().on("zoom", function () {
                        inner.attr("transform", d3.event.transform);
                    });
                    svg.call(zoom);

                    // Create and configure the renderer
                    var render = dagreD3.render();

                    var g;

                    var inputGraph = document.querySelector("#inputGraph");
                    try {
                        g = graphlibDot.read(inputGraph.value);
                    } catch (e) {
                        inputGraph.setAttribute("class", "error");
                        throw e;
                    }

                    g.graph().transition = function (selection) {
                        return selection.transition().duration(500);
                    };

                    // Render the graph into svg g
                    d3.select("svg g").call(render, g);
                </script>
            </h:form>
        </ui:define>
    </ui:composition>
</html>
